﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HP.Cachalote.Repositories.Interfaces;
using HP.Cachalote.Services.Interfaces;
using NUnit.Framework;
using HP.Cachalote.Services;
using HP.Cachalote.UnitTests.FakeRepositories;
using HP.Cachalote.DataModels;

namespace HP.Cachalote.UnitTests.ServiceTests
{
    [TestFixture]
    public class ItemServiceTest
    {
        private IRepositoryFactory _repositoryFactory;
        private IServiceFactory _serviceFactory;
        private ITodoLabelService _labelService;
        private ITodoItemService _itemService;

        [SetUp]
        protected void Setup()
        {
            _repositoryFactory = new FakeRepositoryFactory();
            _serviceFactory = new ServiceFactory(_repositoryFactory);
            _labelService = _serviceFactory.CreateLabelService();
            _itemService = _serviceFactory.CreateTodoItemService();
        }

        [Test]
        public void GetAllTest()
        {
            int count = 3;
            var list = _itemService.GetAll();

            Assert.AreEqual(count, list.Count);
        }

        [Test]
        public void AddTest()
        {
            int lableID = 1;
            int testItemID = 5;

            TodoLabel label = _labelService.Get(lableID);

            TodoItem item = new DataModels.TodoItem
            {
                ID = testItemID,
                CreateOn = DateTime.Now,
                UpdateOn = DateTime.Now,
                ReminderTime = DateTime.Now.AddHours(3),
                DueDate = DateTime.Now.AddHours(3),
                StartTime = DateTime.Now.AddHours(3),
                EndTime = DateTime.Now.AddHours(4),
                IsDismissed = false,
                IsDone = false,
                Label = label,
                NeedReminder = true,
                Note = "Note string 1",
                Priority = 1,
                Title = "todo item New",
            };

            Assert.AreEqual(null, _itemService.Get(testItemID));

            int count = _itemService.GetRowCount(o => o != null);
            _itemService.Add(item);
            int count2 = _itemService.GetRowCount(o => o != null);
            Assert.AreEqual(count + 1, count2);
            Assert.AreSame("todo item New", _itemService.Get(testItemID).Title);
        }

        [Test]
        public void AddOrUpdateTest()
        {
            int labelID = 1;
            int testItemID = 6;

            Assert.AreEqual(null, _itemService.Get(testItemID));
            TodoLabel label = _labelService.Get(labelID);

            int preCount = _itemService.Filter(o => o.Label != null && o.Label.ID == labelID).Count;

            TodoItem item = new DataModels.TodoItem
            {
                ID = testItemID,
                CreateOn = DateTime.Now,
                UpdateOn = DateTime.Now,
                ReminderTime = DateTime.Now.AddHours(3),
                DueDate = DateTime.Now.AddHours(3),
                StartTime = DateTime.Now.AddHours(3),
                EndTime = DateTime.Now.AddHours(4),
                IsDismissed = false,
                IsDone = false,
                Label = label,
                NeedReminder = true,
                Note = "Note string 2",
                Priority = 1,
                Title = "todo item 05",
            };

            _itemService.AddOrUpdate(item);

            int count2 = _itemService.Filter(o => o.Label != null && o.Label.ID == labelID).Count;
            Assert.AreNotEqual(null, _itemService.Get(testItemID));
            Assert.AreEqual(preCount + 1, count2);
            Assert.AreSame("todo item 05", _itemService.Get(item.ID).Title);

            item.Title = "todo item 05 Update";
            _itemService.AddOrUpdate(item);
            int count3 = _itemService.Filter(o => o.Label != null && o.Label.ID == labelID).Count;

            Assert.AreEqual(count2, count3);
            Assert.AreSame("todo item 05 Update", _itemService.Get(item.ID).Title);
        }

        [Test]
        public void DeleteTest()
        {
            int testItemID = 1;

            TodoItem item = _itemService.Get(testItemID);
            _itemService.Delete(item);

            Assert.AreEqual(null, _itemService.Get(testItemID));
            _itemService.Add(item);
            Assert.AreNotEqual(null, _itemService.Get(testItemID));
        }

        [Test]
        public void UpdateTest()
        {
            int testItemID = 1;
            string newName = "New Item";

            TodoItem item = _itemService.Get(testItemID);
            Assert.AreNotEqual(newName, item);

            item.Title = newName;
            _itemService.Update(item);

            TodoItem item2 = _itemService.Get(testItemID);
            Assert.AreEqual(newName, item2.Title);
        }
    }
}